import axios from 'axios'
import { BASE_URL, TIMEOUT } from './request.js'


class HYRequest {
  constructor(baseURL, timeout=10000) {
    this.instance = axios.create({
      baseURL,
      timeout
    })

    this.instance.interceptors.request.use(config => {
    
     return config
    }, err => {
      return err
    })
    this.instance.interceptors.response.use(res => {
      // 处理成功响应
    const statusCode = res.status; // 获取状态码
    if (statusCode === 200) {
      // 登录成功的逻辑处理
      console.log('Login successful', res.status);
    } else {
      // 其他状态码的处理
      console.log('Unexpected status code:', statusCode);
    }
    return Promise.resolve(res,res.status);    
    },
     err => {
      const statusCode = err.response.status; // 获取状态码
      // 错误处理逻辑
      console.log('Error status code:', statusCode);
      return Promise.reject(err);
     
    })
  }

  request(config) {

    return new Promise((resolve, reject) => {
      this.instance.request(config).then(res => {
        resolve(res.data)
 
      }).catch(err => {
        reject(err)

      })
    })
  }

  get(config) {
    return this.request({ ...config, method: "get" })
  }

  post(config) {
    return this.request({ ...config, method: "post" })
  }
}

export default new HYRequest(BASE_URL, TIMEOUT)


